package com.shelpe.services.sinbad.controller.actionLog.impl.campaign;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.CampaignVo;
import com.shelpe.services.sinbad.entity.jpa.AreaList;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CampaignLog;

@Service
public class CampaignUpdateBudgetConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertCampaign(CampaignLog entity, Map<Long, Campaign> campaignMap,List<AreaList> areas) {
		boolean campaignUpdateBudgetConverter = entity.getAction() == CampaignLog.Action.UPDATE_BUDGET;
		if (campaignUpdateBudgetConverter) {
			if (StringUtils.isNotBlank(entity.getNote())) {
				ActionLogDto dto = new ActionLogDto();
				dto.from(entity);
				Campaign campaign = campaignMap.get(entity.getCampaignID());
				CampaignVo vo = this.unserializeCampaign(entity.getNote());
				if (campaign != null && vo != null && vo.getOldBudget() > 0 && vo.getNewBudget() > 0) {
					String[] params = { campaign.getTitle(), vo.getOldBudget() + "", vo.getNewBudget() + "" };
					dto.setReason(messageSource.getMessage("message.campaign.budget.reason", params, Locale.ENGLISH));
					return dto;
				}
			}
		}

		return null;
	}
}
